package com.usaas.search.service.app;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usaas.common.core.domain.AjaxResult;
import com.usaas.search.dto.*;
import com.usaas.search.enums.PlatMessageEnums;
import com.usaas.search.enums.PlatUserPublishEnums;
import com.usaas.search.pojo.PlatCategoryPojo;
import com.usaas.search.pojo.PlatGameInfoPojo;
import com.usaas.search.pojo.PlatMessagePojo;
import com.usaas.search.pojo.PlatUserPublishPojo;
import com.usaas.search.service.IPlatCategoryService;
import com.usaas.search.service.IPlatGameInfoService;
import com.usaas.search.service.IPlatMessageService;
import com.usaas.search.service.IPlatUserPublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AppPlatIndexService {
    @Autowired
    private IPlatGameInfoService platGameInfoService;
    @Autowired
    private IPlatCategoryService categoryService;
    @Autowired
    private IPlatMessageService platMessageService;
    @Autowired
    private IPlatUserPublishService platUserPublishService;

    public AjaxResult indexSearch(IndexSearchDto indexSearchDto) {
        String keyword = indexSearchDto.getSearchKeyword();
        int pageNum = 1;
        int pageSize = 10;
        PlatGameInfoDto platGameInfoDto = new PlatGameInfoDto();
        platGameInfoDto.setGameName(keyword);
        platGameInfoDto.setPageNum(pageNum);
        platGameInfoDto.setPageSize(pageSize);
        Page<PlatGameInfoDto> platGameInfoDtoPage = platGameInfoService.queryPage2(platGameInfoDto);

        PlatCategoryDto platCategoryDto = new PlatCategoryDto();
        platCategoryDto.setCategoryName(keyword);
        platCategoryDto.setPageNum(pageNum);
        platCategoryDto.setPageSize(pageSize);
        Page<PlatCategoryDto> platCategoryDtoPage = categoryService.queryPage2(platCategoryDto);

        Map<String, Object> response = new HashMap<>();
        response.put("game", platGameInfoDtoPage.getRecords());
        response.put("category", platCategoryDtoPage.getRecords());
        return AjaxResult.success(response);
    }

    public AjaxResult rollingNotification(IndexSearchDto indexSearchDto) {
        int pageNum = 1;
        int pageSize = Integer.MAX_VALUE;
        LambdaQueryWrapper<PlatMessagePojo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatMessagePojo::getPlatPromotionType, PlatMessageEnums.PLAT_PROMOTION_TYPE_RUNTHELANTERNNOTICE.getValueStr());

        Page<PlatMessagePojo> page = new Page<>(pageNum, pageSize);
        Page<PlatMessagePojo> pageResult = platMessageService.page(page, wrapper);
        List<PlatMessageDto> platMessageDtos = BeanUtil.copyToList(pageResult.getRecords(), PlatMessageDto.class);
        return AjaxResult.success(platMessageDtos);
    }

    public AjaxResult banners(IndexSearchDto indexSearchDto) {

        int pageNum = 1;
        int pageSize = Integer.MAX_VALUE;
        LambdaQueryWrapper<PlatMessagePojo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatMessagePojo::getPlatPromotionType, PlatMessageEnums.PLAT_PROMOTION_TYPE_ROTATING_ADVERTISING.getValueStr());
        Page<PlatMessagePojo> page = new Page<>(pageNum, pageSize);
        Page<PlatMessagePojo> pageResult = platMessageService.page(page, wrapper);
        List<PlatMessageDto> platMessageDtos = BeanUtil.copyToList(pageResult.getRecords(), PlatMessageDto.class);
        return AjaxResult.success(platMessageDtos);
    }

    public AjaxResult categorys(IndexSearchDto indexSearchDto) {
        String targetKey = indexSearchDto.getTargetKey();
        String targetId = indexSearchDto.getTargetId();
        List<Long> categoryIds = new ArrayList<>();
        if (StrUtil.isNotBlank(targetKey)) {
            Long targetidLong = Long.valueOf(targetId);
            if (targetKey.equals("game")) {
                PlatGameInfoPojo gameInfo = platGameInfoService.getById(targetidLong);
                Long gameCategoryId = gameInfo.getGameCategoryId();
                categoryIds.add(gameCategoryId);
            }
            if (targetKey.equals("category")) {
                categoryIds.add(targetidLong);
            }
        }
        int pageNum = 1;
        int pageSize = Integer.MAX_VALUE;

        LambdaQueryWrapper<PlatCategoryPojo> wrapper = new LambdaQueryWrapper<>();
        Page<PlatCategoryPojo> page = new Page<>(pageNum, pageSize);
        Page<PlatCategoryPojo> pageResult = categoryService.page(page, wrapper);
        return AjaxResult.success(pageResult);
    }

    public AjaxResult platformPromotion(IndexSearchDto indexSearchDto) {
        List<PlatMessageDto> datas = commonSearch(PlatMessageEnums.PLAT_PROMOTION_TYPE_PLATFORMPROMOTION, indexSearchDto);
        return AjaxResult.success(datas);
    }

    public AjaxResult dailyRecommendation(IndexSearchDto indexSearchDto) {
        List<PlatMessageDto> datas = commonSearch(PlatMessageEnums.PLAT_PROMOTION_TYPE_DAILYRECOMMENDATION, indexSearchDto);
        return AjaxResult.success(datas);
    }


    public AjaxResult hot(IndexSearchDto indexSearchDto) {
        List<PlatMessageDto> datas = commonSearch(PlatMessageEnums.PLAT_PROMOTION_TYPE_HOTRECOMMENDATION, indexSearchDto);
        return AjaxResult.success(datas);
    }

    public AjaxResult userPublishList(IndexSearchDto indexSearchDto) {
        int pageNum = 1;
        int pageSize = 10;
        Page<PlatUserPublishPojo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PlatUserPublishPojo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatUserPublishPojo::getPassStatus, PlatUserPublishEnums.PASS_STATUS_APPROVED.getValueLong());
        wrapper.orderByDesc(PlatUserPublishPojo::getId);
        Page<PlatUserPublishPojo> pageResult = platUserPublishService.page(page, wrapper);
        if (pageResult.getRecords() != null && pageResult.getRecords().size() > 0) {
            List<PlatUserPublishDto> platUserPublishDtos = BeanUtil.copyToList(pageResult.getRecords(), PlatUserPublishDto.class);
            return AjaxResult.success(platUserPublishDtos);
        }
        return AjaxResult.success();
    }

    private List<PlatMessageDto> commonSearch(PlatMessageEnums platMessageEnums, IndexSearchDto indexSearchDto) {
        String targetKey = indexSearchDto.getTargetKey();
        String targetId = indexSearchDto.getTargetId();
        List<Long> categoryIds = new ArrayList<>();
        if (StrUtil.isNotBlank(targetKey)) {
            Long targetidLong = Long.valueOf(targetId);
            if (targetKey.equals("game")) {
                PlatGameInfoPojo gameInfo = platGameInfoService.getById(targetidLong);
                Long gameCategoryId = gameInfo.getGameCategoryId();
                categoryIds.add(gameCategoryId);
            }
            if (targetKey.equals("category")) {
                categoryIds.add(targetidLong);
            }
        }

        int pageNum = 1;
        int pageSize = 10;
        LambdaQueryWrapper<PlatMessagePojo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatMessagePojo::getPlatPromotionType, platMessageEnums.getValueStr());
        Page<PlatMessagePojo> page = new Page<>(pageNum, pageSize);
        Page<PlatMessagePojo> pageResult = platMessageService.page(page, wrapper);
        List<PlatMessageDto> platMessageDtos = BeanUtil.copyToList(pageResult.getRecords(), PlatMessageDto.class);
        return platMessageDtos;
    }

}
